using DeviceCommons.DeviceMessages.Serialization;
using Microsoft.Extensions.DependencyInjection;

namespace DeviceCommons.DataHandling
{
    /// <summary>
    /// 支持DI的设备消息序列化器提供服务
    /// </summary>
    public class DeviceMessageSerializerProviderService : IDeviceMessageSerializerProviderService
    {
        private readonly IServiceProvider _serviceProvider;

        public DeviceMessageSerializerProviderService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public IDeviceMessageSerializer MessageSerializer => 
            _serviceProvider.GetRequiredService<IDeviceMessageSerializer>();

        public IDeviceMessageParser MessageParser => 
            _serviceProvider.GetRequiredService<IDeviceMessageParser>();

        public IDeviceMessageHeaderParser HeaderParser => 
            _serviceProvider.GetRequiredService<IDeviceMessageHeaderParser>();

        #region V1 Services

        public IDeviceMessageInfoSerializer InfoV1Serializer =>
            _serviceProvider.GetServices<IDeviceMessageInfoSerializer>()
                .OfType<DeviceMessages.Serialization.V1.Serializers.DeviceMessageInfoSerializer>()
                .FirstOrDefault() ?? 
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Serializers.DeviceMessageInfoSerializer>();

        public IDeviceMessageInfoParser InfoV1Parser =>
            _serviceProvider.GetServices<IDeviceMessageInfoParser>()
                .OfType<DeviceMessages.Serialization.V1.Parsers.DeviceMessageInfoParser>()
                .FirstOrDefault() ??
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Parsers.DeviceMessageInfoParser>();

        public IDeviceMessageInfoReadingsSerializer InfoReadingsV1Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Serializers.DeviceMessageInfoReadingsSerializer>();

        public IDeviceMessageInfoReadingsParser InfoReadingsV1Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Parsers.DeviceMessageInfoReadingsParser>();

        public IDeviceMessageInfoReadingSerializer InfoReadingV1Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Serializers.DeviceMessageInfoReadingSerializer>();

        public IDeviceMessageInfoReadingParser InfoReadingV1Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Parsers.DeviceMessageInfoReadingParser>();

        public IDeviceMessageInfoReadingStatesSerializer InfoReadingStatesV1Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Serializers.DeviceMessageInfoReadingStatesSerializer>();

        public IDeviceMessageInfoReadingStatesParser InfoReadingStatesV1Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Parsers.DeviceMessageInfoReadingStatesParser>();

        public IDeviceMessageInfoReadingStateSerializer InfoReadingStateV1Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Serializers.DeviceMessageInfoReadingStateSerializer>();

        public IDeviceMessageInfoReadingStateParser InfoReadingStateV1Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Parsers.DeviceMessageInfoReadingStateParser>();

        public IDeviceMessageChildSerializer ChildV1Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Serializers.DeviceMessageChildSerializer>();

        public IDeviceMessageChildParser ChildV1Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V1.Parsers.DeviceMessageChildParser>();

        #endregion

        #region V2 Services

        public DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoSerializer InfoV2Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoSerializer>();

        public DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoParser InfoV2Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoParser>();

        public DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingsSerializer InfoReadingsV2Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingsSerializer>();

        public DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingsParser InfoReadingsV2Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingsParser>();

        public DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingSerializer InfoReadingV2Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingSerializer>();

        public DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingParser InfoReadingV2Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingParser>();

        public DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingStatesSerializer InfoReadingStatesV2Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingStatesSerializer>();

        public DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingStatesParser InfoReadingStatesV2Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingStatesParser>();

        public DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingStateSerializer InfoReadingStateV2Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingStateSerializer>();

        public DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingStateParser InfoReadingStateV2Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingStateParser>();

        public DeviceMessages.Serialization.V2.Serializers.DeviceMessageChildSerializer ChildV2Serializer =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Serializers.DeviceMessageChildSerializer>();

        public DeviceMessages.Serialization.V2.Parsers.DeviceMessageChildParser ChildV2Parser =>
            _serviceProvider.GetRequiredService<DeviceMessages.Serialization.V2.Parsers.DeviceMessageChildParser>();

        #endregion
    }

    /// <summary>
    /// 设备消息序列化器提供服务接口
    /// </summary>
    public interface IDeviceMessageSerializerProviderService
    {
        IDeviceMessageSerializer MessageSerializer { get; }
        IDeviceMessageParser MessageParser { get; }
        IDeviceMessageHeaderParser HeaderParser { get; }

        // V1 Services
        IDeviceMessageInfoSerializer InfoV1Serializer { get; }
        IDeviceMessageInfoParser InfoV1Parser { get; }
        IDeviceMessageInfoReadingsSerializer InfoReadingsV1Serializer { get; }
        IDeviceMessageInfoReadingsParser InfoReadingsV1Parser { get; }
        IDeviceMessageInfoReadingSerializer InfoReadingV1Serializer { get; }
        IDeviceMessageInfoReadingParser InfoReadingV1Parser { get; }
        IDeviceMessageInfoReadingStatesSerializer InfoReadingStatesV1Serializer { get; }
        IDeviceMessageInfoReadingStatesParser InfoReadingStatesV1Parser { get; }
        IDeviceMessageInfoReadingStateSerializer InfoReadingStateV1Serializer { get; }
        IDeviceMessageInfoReadingStateParser InfoReadingStateV1Parser { get; }
        IDeviceMessageChildSerializer ChildV1Serializer { get; }
        IDeviceMessageChildParser ChildV1Parser { get; }

        // V2 Services
        DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoSerializer InfoV2Serializer { get; }
        DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoParser InfoV2Parser { get; }
        DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingsSerializer InfoReadingsV2Serializer { get; }
        DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingsParser InfoReadingsV2Parser { get; }
        DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingSerializer InfoReadingV2Serializer { get; }
        DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingParser InfoReadingV2Parser { get; }
        DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingStatesSerializer InfoReadingStatesV2Serializer { get; }
        DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingStatesParser InfoReadingStatesV2Parser { get; }
        DeviceMessages.Serialization.V2.Serializers.DeviceMessageInfoReadingStateSerializer InfoReadingStateV2Serializer { get; }
        DeviceMessages.Serialization.V2.Parsers.DeviceMessageInfoReadingStateParser InfoReadingStateV2Parser { get; }
        DeviceMessages.Serialization.V2.Serializers.DeviceMessageChildSerializer ChildV2Serializer { get; }
        DeviceMessages.Serialization.V2.Parsers.DeviceMessageChildParser ChildV2Parser { get; }
    }
}